home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / txf / src / txfex_p.c < prev    next >
C/C++ Source or Header  |  1994-09-17  |  4KB  |  183 lines

  1. /*====================================================================
  2.  *
  3.  * TXF string exchange module
  4.  *
  5.  *====================================================================
  6.  *                                   copyright(C) 1992-1994 T.Nakatani
  7.  *====================================================================
  8.  */
  9. #include "txf.h"
  10.  
  11. void prepare_exchange(char far **tmpbuf)
  12. {
  13.     int i = 0, j;
  14.     int maxlen = 0, chrattr = CT_ANK;
  15.     char far *form = NULL;
  16.  
  17.     for (i = 0; i < exflg; i++) {
  18.         form = farmalloc(far_strlen(exold[i]) + 1);
  19.         if (form == NULL) {
  20.             errexit("out of memory(old)");
  21.         }
  22.         far_strcpy(form, exold[i]);
  23.         /* free(exold[i]); */
  24.         exold[i] = ftrans(form);
  25.         farfree(form);
  26.         if (maxlen < far_strlen(exold[i])) {
  27.             maxlen = far_strlen(exold[i]);
  28.         }
  29.         exattr[i] = farmalloc(far_strlen(exold[i]) + 1);
  30.         if (exattr[i] == NULL) {
  31.             errexit("out of memory(ex_attr)");
  32.         }
  33.         for (j = 0; *(exold[i] + j) != NUL; j++) {
  34.             chrattr = chkctype(*(exold[i] + j), chrattr);
  35.             *(exattr[i] + j) = chrattr;
  36.         }
  37.         if (exnew[i] != NULL) {
  38.             form = farmalloc(far_strlen(exnew[i]) + 1);
  39.             if (form == NULL) {
  40.                 errexit("Heap Error at(new)");
  41.             }
  42.             far_strcpy(form, exnew[i]);
  43.             /* free(exnew[i]); */
  44.             exnew[i] = ftrans(form);
  45.             farfree(form);
  46.         }
  47.     }
  48.     *tmpbuf = farcalloc(maxlen + 2, 1);
  49.     if (*tmpbuf == NULL) {
  50.         errexit("Heap Error at(new2)");
  51.     }
  52.  
  53.     for (j = 0; j < exflg; j++) {
  54.         exptr[j] = exold[j];
  55.         exattrptr[j] = exattr[j];
  56.     }
  57. }
  58.  
  59. void exchange()
  60. {                /* old→new    */
  61.     int i = 0, j, dmy;
  62.     int sameflg = -1, diff = 0, chrattr = CT_ANK;
  63.     unsigned long int extime = 0, ix, iy;
  64.     char far *rptr;
  65.     char far *tmpbuf;
  66.  
  67.     prepare_exchange(&tmpbuf);
  68.  
  69.     rptr = buffileopen(8);
  70.  
  71.     i = 0;
  72.     ix = 1;
  73.     iy = 1;
  74.     chrattr = CT_ANK;
  75.     while (remainbyte()) {
  76.         sameflg = -1;
  77.         while (sameflg == -1) {
  78.             if (remainbyte()) {
  79.                 rptr = readnext(1);
  80.             }
  81.             else {
  82.                 break;
  83.             }
  84.             if (ix == 1L) {
  85.                 if (chktouch(rptr, iy)) {
  86.                     while (*rptr != LF) {
  87.                         _dos_write(outputh, rptr, 1, &dmy);
  88.                         if (!remainbyte()) break;
  89.                         rptr = readnext(1);
  90.                     }
  91.                     if (*rptr == LF) {
  92.                         _dos_write(outputh, rptr, 1, &dmy);
  93.                         ix = 1;
  94.                         iy++;
  95.                     }
  96.                     continue;
  97.                 }
  98.             }
  99.             chrattr = chkctype(*rptr, chrattr);
  100.             diff = 0;
  101.             for (j = 0; j < exflg; j++) {
  102.                 if ((*rptr == *(exptr[j])) && (chrattr == *(exattrptr[j]))) {
  103.                     exptr[j] += 1;
  104.                     exattrptr[j] += 1;
  105.                     if (*exptr[j] == NUL) {
  106.                         sameflg = j;
  107.                     }
  108.                 }
  109.                 else {
  110.                     if (exold[j] != exptr[j]) {
  111.                         exptr[j] = exold[j];
  112.                         exattrptr[j] = exattr[j];
  113.                     }
  114.                     diff ++;
  115.                 }
  116.             }
  117.             if (sameflg == -1) {
  118.                 if (diff < exflg) { 
  119.                     *(tmpbuf+i) = *rptr;
  120.                     i++;
  121.                     *(tmpbuf+i) = NUL;
  122.                 }
  123.                 else {
  124.                     if (i > 0) {
  125.                         _dos_write(outputh, tmpbuf, i, &dmy);
  126.                     }
  127.                     _dos_write(outputh, rptr, 1, &dmy);
  128.                     i = 0;
  129.                 }
  130.             }
  131.  
  132.             if (*rptr == LF) {
  133.                 ix = 1L;
  134.                 iy++;
  135.                 quote = 0;
  136.             }
  137.             else {
  138.                 ix++;
  139.             }
  140.  
  141.         }
  142.         if (sameflg != -1) {
  143.  
  144.             if ( i - (far_strlen(exold[sameflg]) - 1) > 0) {
  145.                 _dos_write(outputh, tmpbuf,
  146.                         i - (far_strlen(exold[sameflg]) - 1), &dmy);
  147.             }
  148.  
  149.             if (exnew[sameflg] != NUL) {
  150.                 _dos_write(outputh, exnew[sameflg], far_strlen(exnew[sameflg]), &dmy);
  151.             }
  152.             exptr[sameflg] = exold[sameflg];
  153.             exattrptr[sameflg] = exattr[sameflg];
  154.             i = 0;
  155.             extime++;
  156.             if (viewmode > 1) fprintf(stderr, "%ld\r", extime);
  157.         }
  158.         else if (i > 0) {
  159.             _dos_write(outputh, tmpbuf, i, &dmy);
  160.         }
  161.         i = 0;
  162.     }
  163.     if (viewmode>0) {
  164.         fprintf(stderr, "%ldtime(s) exchanged!\n", extime);
  165.     }
  166.  
  167.     bufclose();
  168.     farfree(tmpbuf);
  169.     for (j = 0; j < exflg; j++) {
  170.         farfree(exattr[j]);
  171.     }
  172. }
  173.  
  174. void exchangedriver()
  175. {
  176.     if (viewmode > 1)
  177.         fprintf(stderr, "TXF Super exchange module.(Parallel)\n");
  178.  
  179.     if (exold[0] != NULL) {
  180.         exchange();
  181.     }
  182. }
  183.